package com.ysd.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.exception.RRException;
import com.ysd.modules.app.dao.OptionsDao;
import com.ysd.modules.app.dao.OptionsRecordsDao;
import com.ysd.modules.app.dao.TopicDao;
import com.ysd.modules.app.entity.OptionsEntity;
import com.ysd.modules.app.entity.OptionsRecordsEntity;
import com.ysd.modules.app.entity.StudentNote;
import com.ysd.modules.app.entity.TopicEntity;
import com.ysd.modules.app.service.StudentNoteService;
import com.ysd.modules.app.service.TopicsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TopicsServiceImpl extends ServiceImpl<TopicDao, TopicEntity> implements TopicsService {

    @Resource
    private OptionsDao optionsDao;
    @Resource
    private OptionsRecordsDao optionsRecordsDao;
    @Autowired
    private StudentNoteService studentNoteService;

    /**
     * 根据章节id获取章节下的题目
     * 采用业务拼接实体类
     *
     * @param cid
     * @return
     */
    @Override
    public List<TopicEntity> getTopicList(Integer cid) {
        QueryWrapper<TopicEntity> qw = new QueryWrapper<>();
        if (cid == null || cid == 0) {
            throw new RRException("章节id不能为空或者0");
        }
        qw.eq("chapter_id", cid);
        qw.eq("status", 1);
        List<TopicEntity> topicList = baseMapper.selectList(qw);
        if (!topicList.isEmpty()) {
            for (int i = 0; i < topicList.size(); i++) {
                TopicEntity topicEntity = topicList.get(i);
                QueryWrapper<OptionsEntity> qw1 = new QueryWrapper<>();
                qw1.eq("topic_id", topicEntity.getId());
                List<OptionsEntity> optionsList = optionsDao.selectList(qw1);
                topicEntity.setOptionsList(optionsList);
            }
        }
        return topicList;
    }

    /**
     * @param cid
     * @return
     */
    @Override
    public List<TopicEntity> getWrongTopicList(Integer stuId, Integer cid, Integer type) {
        if (cid == null || cid == 0) {
            throw new RRException("章节id不能为空或者0");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("stu_id", stuId);
        map.put("chapter_id", cid);
        map.put("type", type);
        List<TopicEntity> topicList = baseMapper.getWrongTopicList(map);
        if (!topicList.isEmpty()) {
            for (int i = 0; i < topicList.size(); i++) {
                TopicEntity topicEntity = topicList.get(i);
                QueryWrapper<OptionsEntity> qw1 = new QueryWrapper<>();
                qw1.eq("topic_id", topicEntity.getId());
                List<OptionsEntity> optionsList = optionsDao.selectList(qw1);
                topicEntity.setOptionsList(optionsList);
                /**
                 * 查询做题记录(默认为章节练习)
                 *  题目ID,学生ID,类型
                 */
                QueryWrapper<OptionsRecordsEntity> ore = new QueryWrapper<>();
                ore.eq("stu_id", stuId);
                ore.eq("topic_id", topicEntity.getId());
                ore.eq("type", type);
                OptionsRecordsEntity optionsRecordsEntity = optionsRecordsDao.selectOne(ore);
                if (optionsRecordsEntity != null) {
                    topicEntity.setMyOptionId(optionsRecordsEntity.getOptionsId());
                    topicEntity.setAnswer(true);
                    if (topicEntity.getAnswerId().equals(optionsRecordsEntity.getOptionsId())) {
                        topicEntity.setSuccFlag(true);
                    }
                }
                /**
                 * 笔记信息
                 */
                List<StudentNote> studentNoteList = studentNoteService.studentNoteList(topicEntity.getId().intValue());
                topicEntity.setStudentNoteList(studentNoteList);
            }
        }
        return topicList;
    }

    /**
     * 根据章节 重做错题
     *
     * @param stuId
     * @param cid
     * @return
     */
    @Override
    public List<TopicEntity> renewalWrongTopicList(Integer stuId, Integer cid) {
        if (cid == null || cid == 0) {
            throw new RRException("章节id不能为空或者0");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("stu_id", stuId);
        map.put("chapter_id", cid);
        List<TopicEntity> topicList = baseMapper.getWrongTopicList(map);
        if (!topicList.isEmpty()) {
            for (int i = 0; i < topicList.size(); i++) {
                TopicEntity topicEntity = topicList.get(i);
                QueryWrapper<OptionsEntity> qw1 = new QueryWrapper<>();
                qw1.eq("topic_id", topicEntity.getId());
                List<OptionsEntity> optionsList = optionsDao.selectList(qw1);
                topicEntity.setOptionsList(optionsList);
            }
        }
        return topicList;
    }
}
